<!DOCTYPE HTML>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
		<link rel="Bookmark" href="/favicon.ico">
		<link rel="Shortcut Icon" href="/favicon.ico" />
		<link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
		<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
		<link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.8/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/skin/default/skin.css" id="skin" />
		<link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
		<link rel="stylesheet" href="../lib/layui/css/layui.css">

		<title>授权信息</title>
	</head>

	<body>
		<div class="page-container">
			<form id="addForm" class="layui-form" lay-filter="addForm">
				<input type="hidden" id="ftoken" name="token" />
				<input type="hidden" id="ftokenUserId" name="tokenUserId" />
				<input type="hidden" id="role_id" name="roleId" />

				<div id="sysUserList">
				</div>

				<div class="layui-form-item text-c">
					<button class="layui-btn" lay-submit="" lay-filter="btnSubmit">保存</button>
				</div>
			</form>
		</div>
		<div id="pager" class="clearfix"></div>
		<!--_footer 作为公共模版分离出去-->
		<script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script>
		<script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
		<script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
		<script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script>
		<!--/_footer 作为公共模版分离出去-->
		<script type="text/javascript" src="../lib/layui/layui.js"></script>
		<script type="text/javascript" src="../static/js/common.js"></script>
		<!--请在下方写此页面业务相关的脚本-->
		<script type="text/javascript">
			var role_id = parent.$("#role_id").val();
			layui.use(['form'], function() {

				var form = layui.form;
				//选中记录
				var checkedArr = new Array();
				var uncheckedArr = new Array();

				$("#role_id").val(role_id);
				$("#ftoken").val(token);
				$("#ftokenUserId").val(userId);

				if(role_id) {
					//获取授权信息
					var data = {
						"token": token,
						"tokenUserId": userId,
						"roleId": role_id
					};
					$("#sysUserList").html("");
					request_ajax("POST", admin_url + "/scaUserRole/getUserAuthInfo", data, function(result) {
						if(result.code == 200) {
							var list = result.data;
							for(var i = 0; i < list.length; i++) {
								var checked = "";
								if(list[i].auth != null) {
									checked = "checked='checked'";
									checkedArr.push(list[i].id.toString());
								}
								var html = '<input lay-filter="m_checked" value="' + list[i].id + '"  type="checkbox"  title="' + list[i].name + '" ' + checked + '>';
								$("#sysUserList").append(html);
							}
							//重新渲染
							form.render('checkbox');
						}
					});
				}

				form.on('checkbox(m_checked)', function(e) {
					if(e.elem.checked) {
						var index = uncheckedArr.indexOf(e.value);
						if(index > -1) {
							uncheckedArr.splice(index, 1);
						}
						checkedArr.push(e.value);
					} else {
						var index = checkedArr.indexOf(e.value);
						if(index > -1) {
							uncheckedArr.push(e.value);
							checkedArr.splice(index, 1);
						}
					}
				})

				//监听提交
				form.on('submit(btnSubmit)', function(e) {
					var data = {
						"addUseIds": checkedArr.toString(),
						"delUserIds": uncheckedArr.toString(),
						"token": token,
						"tokenUserId": userId,
						"roleId": role_id
					};
					request_ajax("POST", admin_url + "/scaUserRole/oprUserRole", data, function(result) {
						layer.msg(result.msg, {
							time: 800,
							end: function() {
								var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
								parent.layer.close(index); //再执行关闭   
								parent.tableReload();
							}
						});
					});
					return false;
				});

			});
		</script>
	</body>

</html>